﻿using System.Collections;
using System.Collections.Generic;

namespace AppMain
{
    public class RoleMoveWidget : IMoveWidget
    {
        protected Dictionary<RoleMove_Status, IMoveProcesser> pool;
        private RoleMove_Status roleMoveStatus = RoleMove_Status.Stand;
        private IMoveProcesser currProcesser;
        private BoxRole agent;

        protected virtual void initProcesser()
        {
            pool = new Dictionary<RoleMove_Status, IMoveProcesser>();
            pool.Add(RoleMove_Status.Stand, new TMStandProcesser());
            pool.Add(RoleMove_Status.Run, new TMRunProcesser());
            pool.Add(RoleMove_Status.HitBack, new TMHitBackProcesser());
            //pool.Add(RoleMove_Status.HirAir, new TMHitAirProcesser());
            pool.Add(RoleMove_Status.HitCurve, new TMHitCurveProcesser());
            //pool.Add(RoleMove_Status.HitFall, new TMHitFallProcesser());
            pool.Add(RoleMove_Status.JumpUp, new TMJumpUpProcesser());
            pool.Add(RoleMove_Status.JumpFall, new TMJumpFallProcesser());
            pool.Add(RoleMove_Status.LieDown, new TMLieDownProcesser());
            pool.Add(RoleMove_Status.LieUp, new TMLieUpProcesser());
            pool.Add(RoleMove_Status.SelfMove, new TMSelfMoveProcesser());
            pool.Add(RoleMove_Status.HitCurveRebound, new TMHitCurveRebound());
        }

        public void initialize(BoxRole role)
        {
            this.agent = role;
            //添加处理器
            initProcesser();
            //处理器初始化
            var ier = pool.GetEnumerator();
            while (ier.MoveNext())
            {
                ier.Current.Value.init(agent);
            }
            currProcesser = pool[RoleMove_Status.Stand];
            currProcesser.enter(new MoveData(), RoleMove_Status.Stand);
        }
        public void doMove(MoveData moveData)
        {
            Move_Status status = moveData.moveStatus;
            //最终状态 = 玩家当前状态+请求的状态
            //例如: 当前stand+HitAuto = hitBack
            int uid = (int)roleMoveStatus * 1000 + (int)status;
            //sql查询
            string sceneSql = string.Format("select *from tb_movestatus_temp where uid='{0}'", uid);
            var Reader = SqlMgr.Instance.select(sceneSql);
            int resultState = 0;
            if (Reader.Read())
            {
                resultState = (int)Reader[1];
            }
            Reader.Close();

            if (resultState > 0)
            {
                RoleMove_Status result = (RoleMove_Status)resultState;
                bool isNew = roleMoveStatus != result;
                //把服务器实体状态同步给客户端
                pb.StatusMsg msg = new pb.StatusMsg();
                msg.roleId = this.agent.UID;
                msg.roleStatus = resultState;
                msg.distance = moveData.distance;
                msg.height = moveData.height;
                msg.speed = moveData.speed;
                msg.time = moveData.time;
                msg.dir = new pb.PBVector3();
                msg.dir.x = moveData.dir.x;
                msg.dir.y = moveData.dir.y;
                msg.dir.z = moveData.dir.z;
                msg.pos = new pb.PBVector3();
                msg.pos.x = agent.box3D.posX;
                msg.pos.y = agent.box3D.posY;
                msg.pos.z = agent.box3D.posZ;
                msg.param1 = isNew ? 0 : 1;
                this.agent.AttrMap.getAttr<BaseScene>(RoleAttr.Scene).Value.broadCast(Cmd.StatusMsg, msg);
                if (isNew)
                {
                    RoleMove_Status lastStatus = roleMoveStatus;
                    currProcesser.exit(result);
                    roleMoveStatus = result;
                    currProcesser = pool[roleMoveStatus];
                    currProcesser.enter(moveData, lastStatus);
                }
                else
                {
                    currProcesser.update(moveData);
                }
            }
            else
            {
                Logger.logError(string.Format("MoveWidget找到配置 reqState: {0}  currState: {1}", status, roleMoveStatus));
            }
        }

        public RoleMove_Status getRoleMoveStatus()
        {
            return roleMoveStatus;
        }

        public void input(float x, float z)
        {
            this.currProcesser.input(x, z);
        }

        public void input(float value)
        {
            this.currProcesser.input(value);
        }

        public void tick(float frameTime)
        {
            this.currProcesser.tick(frameTime);
        }

        public void forceTo(RoleMove_Status status)
        {
            if (roleMoveStatus != status)
            {
                RoleMove_Status lastStatus = roleMoveStatus;
                currProcesser.exit(status);
                roleMoveStatus = status;
                currProcesser = pool[roleMoveStatus];
                currProcesser.enter(new MoveData(), lastStatus);
            }
            else
            {
                currProcesser.update(new MoveData());
            }
        }

        public void forceTo(RoleMove_Status status, MoveData moveData)
        {
            if (roleMoveStatus != status)
            {
                RoleMove_Status lastStatus = roleMoveStatus;
                currProcesser.exit(status);
                roleMoveStatus = status;
                currProcesser = pool[roleMoveStatus];
                currProcesser.enter(moveData, lastStatus);
            }
            else
            {
                currProcesser.update(moveData);
            }
        }
    }
}

